load("classpath:ext/git.js");

function gitToolUsage() {
    console.info("usage: ");
    console.info("    ljs git ssh");
    console.info("    ljs git clone localPath remotePath(git@...)");
    console.info("    ljs git pull [localPath]");
    console.info("    ljs git add [localPath]");
    console.info("    ljs git commit [localPath] message");
    console.info("    ljs git status [localPath]");
    console.info("    ljs git push [localPath]");
}

function gitMain(params) {
    if (params.length < 2) {
        gitToolUsage();
        return;
    }

    var cmd = params[1].trim();

    if ("ssh" === cmd) {
        console.info(getResourceContent("classpath:/ssh/id_rsa.pub"));
        return;
    }
    var localPath = "./";
    if (params.length > 2) {
        localPath = params[2].trim();
    }
    var client = new GitClient(localPath);

    if ("clone" === cmd) {
        if (params.length < 4) {
            gitToolUsage();
            return;
        }
        client.cloneGit(params[3]);
        return;
    }

    if ("pull" === cmd) {
        client.pullGit();
        return;
    }

    if ("commit" === cmd) {
        if (params.length < 3) {
            gitToolUsage();
            return;
        }
        client.localPath = "./";
        var commitMsg = params[2];

        if (params.length > 3) {
            client.localPath = params[2];
            commitMsg = params[3];
        }
        client.commitGit(commitMsg);
        return;
    }

    if ("push" === cmd) {
        client.pushGit();
        return;
    }

    if ("status" === cmd) {
        client.statusGit();
        return;
    }

    if ("add" === cmd) {
        client.addGit();
        return;
    }

    if ("addAll" === cmd) {
        client.addAllGit();
        return;
    }
    console.info("error on cmd=" + cmd)
}

gitMain(args);